local skel = fk.CreateSkill {
  name = "emo__chongzhen",
  tags = {Skill.Limited},
}

Fk:loadTranslationTable{
  ["emo__chongzhen"] = "重振",
  [":emo__chongzhen"] = "限定技，当你进入濒死状态时，你可以失去〖附和〗/〖热忱〗/〖捉襟〗中至少一个技能，增加X-1点体力上限，并回复X点体力，然后从随机5个武将牌中获得至多X个技能（X为你失去的技能数）。",

  ["#emo__chongzhen-invoke"] = "重振：你可以失去“附和”、“热忱”、“捉襟”中任意个",
  ["#emo__chongzhen-lose"] = "重振：选择失去的技能，回复体力，并获得等量新技能",
  ["#emo__chongzhen-skill"] = "重振：选择 %arg 个技能获得",

  ["$emo__chongzhen1"] = "我是不会放弃的",
  ["$emo__chongzhen2"] = "我应该也可以的",
}

local U = require "packages.utility.utility"

skel:addEffect(fk.EnterDying, {
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(skel.name) and target == player and player:usedSkillTimes(skel.name, Player.HistoryGame) == 0
    and (player:hasShownSkill("emo__fuhe",  true) or player:hasShownSkill("emo__rechen",  true) or player:hasShownSkill("emo__zhuojin",  true))
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askToSkillInvoke(player, {skill_name = skel.name, prompt = "#emo__chongzhen-invoke"})
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local skills = table.filter({"emo__fuhe","emo__rechen","emo__zhuojin"}, function (s)
      return player:hasShownSkill(s, true)
    end)
    if #skills == 0 then return end
    local choices = room:askToChoices(player, {
      min_num = 1, max_num = #skills, skill_name = skel.name, detailed = true, choices = skills, cancelable = false,
      prompt = "#emo__chongzhen-lose",
    })
    room:handleAddLoseSkills(player, "-"..table.concat(choices, "|-"))
    room:changeMaxHp(player, #choices - 1)
    if not player.dead and player:isWounded() then
      room:recover { num = #choices, skillName = skel.name, who = player, recoverBy = player}
    end
    if player.dead then return end
    -- 只从部分官方扩展包获取技能
    local officialPackage = {"jsrg","tenyear", "mobile", "mougong", "ol", "overseas", "sp", "standard_ex", "standard"}
    local generals = {}
    for _, name in pairs(room.general_pile) do
      local general = Fk.generals[name]
      if general.package and table.contains(officialPackage, general.package.extensionName) then
        table.insert(generals, name)
      end
    end
    if #generals == 0 then return end
    generals = table.random(generals, 5)
    local general_skills, all_skills = {}, {}
    for _, g in ipairs(generals) do
      local list = {}
      for _, s in ipairs(Fk.generals[g]:getSkillNameList()) do
        if not player:hasSkill(s, true) and not table.contains(all_skills, s) then
          table.insert(list, s)
          table.insert(all_skills, s)
        end
      end
      table.insert(general_skills, list)
    end
    if #all_skills == 0 then return end
    local num = math.min(#choices, #all_skills)
    local togain = U.askToChooseGeneralSkills(player, {
      generals = generals,
      skills = general_skills,
      min_num = num,
      max_num = num,
      skill_name = skel.name,
      prompt = "#emo__chongzhen-skill:::"..#choices,
      cancelable = false,
    })
    if #togain > 0 then
      room:handleAddLoseSkills(player, table.concat(togain, "|"))
    end
  end,
})

return skel
